//
//  AppDelegate.swift
//  SinaWeibo_Swift
//
//  Created by TianZhen on 16/4/8.
//  Copyright © 2016年 TianZhen. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    
    var window: UIWindow?
    
    
    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
        UINavigationBar.appearance().tintColor = UIColor.orangeColor()
        
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        
        // 新特性界面
        window?.rootViewController = defaultController()
        
        window?.makeKeyAndVisible()
        
        return true
    }
    
    // MARK: - 程序启动后默认的控制器
    private func defaultController() -> UIViewController {
        // 没有登录直接返回MainViewController
        if !WBUserAccountViewModel.sharedInstance.userLogin {
            return WBTabBarController()
        }
        // 判断是否是新版本
        return isNewVersion() ? WBNewFeatureViewController() : WBWelcomeViewController()
    }
    
    // 判断版本号
    private func isNewVersion() -> Bool
    {
        // 获取版本号
        let currentVersion = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String
        
        let sandboxVersionKey = "sandboxVersionKey"
        // 上次版本号
        let sandboxVersion = NSUserDefaults.standardUserDefaults().stringForKey(sandboxVersionKey)
        
        // 判断两个版本是否相同
        let newVersion = currentVersion != sandboxVersion
        // 如果是新版本，保存版本信息
        if newVersion
        {
            NSUserDefaults.standardUserDefaults().setObject(currentVersion, forKey: sandboxVersionKey)
            NSUserDefaults.standardUserDefaults().synchronize()
        }
        return newVersion
    }
    
    // MARK: - 切换控制器
    /**
    切换控制器
    parameter isMain: 是否是MainViewController
    */
    func switchViewController(isMain: Bool) {
        let controller = isMain ? WBTabBarController() : WBWelcomeViewController()
        window?.rootViewController = controller
    }
    
}

